<?php
require "../../system/framework.php";
require_once("../../system/api/simplehtmldom/simple_html_dom.php");


// user check


$contentId = $_REQUEST['content_id'];
//$htmlId = $_REQUEST['html_id'];



 $data=db::getDataLine("content", "id='$contentId'");   



// falls Save
//if (isset($_REQUEST['content_id']))
if (isset($_REQUEST['save_button']))
    {

        
        $contentId=request::post("content_id");
        $contentText=stripslashes(request::post("content_text"));

        //echo $content;
        //exit;
        
        //$title=request::post("title"); 
        //$template=request::post("template"); 
        //$usergroup=request::post("usergroup"); 
        //echo $title;
        //exit;
        
        
        
        
        $data=db::getDataLine("content", "id='$contentId'");   
        
        
        $content = new content();
        //$content->url = $data['_url'];
        $content->text = $contentText;
        $content->contentId = $contentId;
        $url = $content->createContent();
        
        
        
        
        
        //http::redirect($url);   
        http::redirect($data['_url']);   
        
        
        
        
  /*      $contentId=request::post("content_id");
        $content=stripslashes(request::post("content_text"));

        //echo $content;
        //exit;
        
        $title=request::post("title"); 
        $template=request::post("template"); 
        $usergroup=request::post("usergroup"); 
        //echo $title;
        //exit;
        
        $data=db::getDataLine("content", "id='$contentId'");

        $filename=$data['content_filename'];
        //$text=request::post("text");

        $contentOrginal=file::readTextFile($filename, false);
        //$contentOrginal = dom::setTag("body", $content, $contentOrginal);
        
        // Werte im Orginal File speichern
        
        
        $contentOrginal=dom::replaceInnerHtml("body", $content, $contentOrginal);
        $contentOrginal=dom::replaceInnerHtml("title", $title, $contentOrginal);
   /*     $contentOrginal=dom::replaceInnerHtml("template", $template, $contentOrginal);
        $contentOrginal=dom::replaceInnerHtml("usergroup", $usergroup, $contentOrginal); */

        
        
   //     file::createTextFile($filename, $contentOrginal, false);
        
        
        
       // $content = new content();
       // $content->
        
        
        
        
        
        
 /*        $compileContentFile=new compile_content_file();
         $compileContentFile->baseDir=false;

                //$contentPath =

                //$compileContentFile->contentPath = config::$projectPath."/content";
         $compileContentFile->contentPath=$data['content_path'];

         $compileContentFile->compileContentFile($data['content_filename'],
                    config::$basePath . $data['code_filename']);

                    
                    // falls ein Fehler gefunden wurde
                    if (compile_status::$foundError) {
                        compile_status::printErrorMessage();
                        exit;
                    }
        
            */
        
        
        
        
        
        //exit;


        /*$content = new content();
                                   //$content->title = $title;
                                   //$content->url = $url;
                                   $content->contentId = $data['id'];   
                                   $content->text = $content;
                                   /*$content->contentFilename = $filenameFrom;
                                   $content->template = $template;
                                   $content->header = $header;*/

        /* $content->compile = true;
         $content->createPage();   */

       /* $compileText=new compile_text();
        //$compileText->phpScriptPath=$this->phpScriptPath;
        $compileText->codeFilename=$data['code_filename'];
        //$compileText->systemPath=$this->systemPath;

        $compileText->pageUrl=$data['_url'];
        $text=$compileText->compileText($content);

        // Body Text speichern
        //$fileName=config::$distributionPath . "/code/content/page_text_" . $codeFilename . ".php";
        file::createTextFile(config::$basePath . $data['code_filename'], $text, false);*/


        // Header speichern
        /* $fileName=config::$distributionPath . "/code/content/page_text_" . $codeFilename . "_header.php";
         //$headerContent = $data['header'].$replaceCom->header;
         $headerContent=$this->header . $compileText->header;
         file::createTextFile($fileName, $headerContent, false);   */


        /*  $compileContentFile = new compile_content_file();
                 $compileContentFile->baseDir=$this->baseDir;
                 $compileContentFile->contentPath = $this->contentPath;
                 //$compileContentFile->
                 $compileContentFile->compileContentFile($folderItem['dir']); */

                 
                 
        // Title speichern
    /*    $dataContent = array();
        $dataContent['title'] = $title;
        db::saveData("content", $dataContent, $contentId);
                 
                 
                 
                 
                 
        //$url=config::$baseUrl . $data['_url'];
        $url=$data['_url'];   */
        
        
        
        
        
      
    }



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Text Editor</title>
<script src="<?php echo config::$baseUrl;?>/api/ckeditor/ckeditor.js"></script>


<script type="text/javascript">
    window.onload = function()
    {
        CKEDITOR.replace('text');
    };
</script>

</head>
<body>
       
<?php
    //require "../../system/framework.php";
    $html=file::readTextFile($data['content_filename'], true); 
    $text = re::getHtmlTagValue("body", $html);
    //$title = re::getHtmlTagValue("title", $html);
  
 /*  $template = re::getHtmlTagValue("template", $html);
  if ($template == "") {
      $template = "index.html";
  }
  
  
  $usergroup = re::getHtmlTagValue("usergroup", $html);
  if ($usergroup == "") {
      $usergroup = "0";
  } */
  
  
  
  // mssen umgewandelt werden, da sonst die ganze seite im ckeditor angezeigt wird 
$text = str_replace("<", "&lt;", $text);
$text = str_replace(">", "&gt;", $text);      

//$dom=str_get_html($data['text']);
/*$dom=str_get_html($contentOrginal);

//$domArray=$dom->find("[com=text_editor]");
$domArray=$dom->find("[id=$htmlId]");

foreach ($domArray as $item)
    {
    $text=$item->innertext;
    } */


//    print_r($data);

?>

<h1><?php echo $data['title'];?></h1>


<form action = "" method="post">
   <!-- <p>Titel<br />
      <label for="textfield"></label>
      <input type="text" name="title" id="title" value="<?php echo $title;?>" />
    </p>-->
    <!--<p>Template<br />
      <label for="select"></label>
      <select name="template" id="template">
      <?php
    foreach (db::getData("_template", "", "template") as $dataTemplate) {
?>
      <option value="<?php echo $dataTemplate['template'];?>"<?php if (strtolower($template) == strtolower($dataTemplate['template'])) echo ' selected="selected"';?>><?php echo $dataTemplate['template'];?></option>
      <?php
    }
?>
      </select>
    </p>
    <p>Usergroup<br />
      <label for="select2"></label>
      <select name="usergroup" id="usergroup">
      <?php
    foreach (db::getData("usergroup", "", "usergroup_number") as $dataUsergroup) {
?>
      <option value="<?php echo $dataUsergroup['usergroup_number'];?>"<?php if (strtolower($usergroup) == strtolower($dataUsergroup['usergroup_number'])) echo ' selected="selected"';?>><?php echo $dataUsergroup['usergroup_number']." ".$dataUsergroup['usergroup'];?></option>
      <?php
    }
?>
      </select>
    </p>
    <p>(ein und ausblenden, anzeigen von url)</p>-->
    <p>
      <textarea name="content_text" id="text" cols = "30" rows = "30">
    <?php echo $text; ?>
    </textarea>
      
      <input name = "content_id" type = "hidden" value = "<?php echo $contentId;?>" />
      <input name = "html_id" type = "hidden" value = "<?php echo $htmlId;?>" />    
      
      <input type = "submit" name = "save_button" value = "Save" />
    </p>
</form>



</body>
</html>
